﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTO;
using BUS;
using DAO;

namespace QuanLySach
{
    /// <summary>
    /// Interaction logic for LoginDialog.xaml
    /// </summary>
    public partial class LoginDialog : Window
    {
        public Section_DTO Section { get; set; }
        public LoginDialog()
        {
            InitializeComponent();
            TaiKhoan.Focus();
            server.Text = SQLConnectData.Server;
        }

        private void DangNhapButton_Click(object sender, RoutedEventArgs e)
        {
            // if it ok
            try
            {
                int taikhoan = Int32.Parse(TaiKhoan.Text);
                string matkhau = MatKhau.Text;
                SQLConnectData.Server = server.Text;
                SQLConnectData._data = null;
                NhanVien_DTO resultNV = NhanVien_BUS.NHANVIEN.LayNhanVien(taikhoan);
                resultNV.MaNhanVien = taikhoan;
                if (resultNV == null)
                {
                    MessageBox.Show("Tai khoan khong ton tai");
                    return;
                }

                resultNV.MatKhau = resultNV.MatKhau.Trim();
                if (resultNV.MatKhau != matkhau)
                {
                    MessageBox.Show("Mat khau khong dung");
                    return;
                }

                // Đăng nhập thành công
                // Kiểm tra Vị trí
                ViTri_DTO vt = ViTri_BUS.VITRI.LayQuyenHan(resultNV.MaViTri);
                Section = new Section_DTO(resultNV.MaNhanVien, resultNV.HoTen, resultNV.MatKhau, vt);

                this.Close();
            }
            catch (FormatException)
            {
                MessageBox.Show("Tai khoan ko dung dinh dang");
            }
            catch (Exception)
            {
            }
        }

        private void ThoatButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void server_GotFocus(object sender, RoutedEventArgs e)
        {
            server.SelectAll();
        }
    }
}
